﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using VNet.VisiSuite.Common.Logger;

namespace VNet.VisiSuite.Controls
{
    /// <summary>
    /// Interaction logic for ToolbarControl.xaml
    /// </summary>
    public partial class ToolbarControl
    {

        #region Dependency Properties

        public static DependencyProperty RightTilesProperty = DependencyProperty.Register("RightTiles", typeof(ObservableCollection<VNetMenuItem>), typeof(ToolbarControl),
                new PropertyMetadata(onRightTilesChanged));

        public ObservableCollection<VNetMenuItem> RightTiles
        {
            get { return (ObservableCollection<VNetMenuItem>)GetValue(RightTilesProperty); }
            set { SetValue(RightTilesProperty, value); }
        }

        private static void onRightTilesChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            var toolbar = source as ToolbarControl;
            var tiles = e.NewValue as ObservableCollection<VNetMenuItem>;

            toolbar._right_tiles.ItemsSource = tiles;
            toolbar._right_tiles.Items.Refresh();
        }

        public static DependencyProperty LeftTilesProperty = DependencyProperty.Register("LeftTiles", typeof(ObservableCollection<VNetMenuItem>), typeof(ToolbarControl),
        new PropertyMetadata(onLeftTilesChanged));

        public ObservableCollection<VNetMenuItem> LeftTiles
        {
            get { return (ObservableCollection<VNetMenuItem>)GetValue(LeftTilesProperty); }
            set { SetValue(LeftTilesProperty, value); }
        }

        private static void onLeftTilesChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            var toolbar = source as ToolbarControl;
            var tiles = e.NewValue as ObservableCollection<VNetMenuItem>;

            toolbar._left_tiles.ItemsSource = tiles;
            toolbar._left_tiles.Items.Refresh();
        }

        public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ToolbarControl),
        new PropertyMetadata(onTitleChanged));

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        private static void onTitleChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            var toolbar = source as ToolbarControl;
            var title = e.NewValue as string;

            toolbar.Title = title;
            toolbar.NotifyPropertyChanged("Title");
        }

        #endregion

        public ToolbarControl()
        {
            InitializeComponent();
            _toolbar.DataContext = this;
        }
        
        public void SetTiles(string title, IList<VNetMenuItem> leftMenuItems, IList<VNetMenuItem> rightMenueItems)
        {
            Title = title;
            NotifyPropertyChanged("Title");

            _left_tiles.ItemsSource = leftMenuItems;
            _left_tiles.Items.Refresh();

            _right_tiles.ItemsSource = rightMenueItems;
            _right_tiles.Items.Refresh();
        }

        // SelectionChanged does NOT get called if we click on the already selected item in the list.
        private void onSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var list = sender as ListView;
                var item = (VNetMenuItem)list.SelectedItem;
                if (item != null)
                {
                    if (item.IsEnabled)
                        item.Selected();
                    list.SelectedItem = null;
                }
            }
            catch (Exception ex)
            {
                FileLog.Error("OnPanelSelection() - ", ex);
            }
        }

        // PreviewLeftMouseUp only gets called if we click on the already selected item in the list.
        private void onLeftMouseUp(object sender, RoutedEventArgs e)
        {
            try
            {
                var list = sender as ListView;
                var item = (VNetMenuItem)list.SelectedItem;
                if (item != null)
                {
                    item.Selected();
                    if (item.IsEnabled)
                        list.SelectedItem = null;
                }
            }
            catch (Exception ex)
            {
                FileLog.Error("onLeftMouseUp() - ", ex);
            }
        }
        
    }
}
